package cn.com.easttech.gofishing.util;

import android.app.ProgressDialog;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * Created by jianl on 2017/12/17.
 */

public class HttpUtil {

    public static void sendGetRequest(final String url, final Map<String, String> parameter, final HttpCallBackListener listener) {
        sendRequest(url, parameter, "GET", listener);
    }

    public static void sendPostRequest(final String url, final Map<String, String> parameter, final HttpCallBackListener listener) {
        sendRequest(url, parameter, "POST", listener);
    }

    public static void sendRequest(final String url, final Map<String, String> parameter, final String method, final HttpCallBackListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    Log.i("HttpUtil", url);

                    connection = (HttpURLConnection) new URL(url).openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(10000);
                    connection.setReadTimeout(10000);

                    int rsponseCode = connection.getResponseCode();
                    Log.i("HttpUtil", "rsponseCode:" + String.valueOf(rsponseCode));

                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                    StringBuilder responseText = new StringBuilder();

                    String line;
                    while ((line = reader.readLine()) != null) {
                        responseText.append(line);
                    }

                    if (listener != null) {
                        //回调onFinish方法
                        listener.onFinish(responseText.toString());
                    }
                } catch (Exception e) {
                    if (listener != null) {
                        //回调onError方法
                        listener.onError(e);
                    }
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

    public static File getFileFromServer(String appUrl, String appName, ProgressDialog progressDialog) throws Exception {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            URL url = new URL(appUrl);
            HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            progressDialog.setMax(conn.getContentLength());
            InputStream is = conn.getInputStream();
            File file = new File(Environment.getExternalStorageDirectory(), appName);
            FileOutputStream fos = new FileOutputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len ;
            int total=0;
            while((len = bis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                total+= len;
                progressDialog.setProgress(total);
            }
            fos.close();
            bis.close();
            is.close();
            return file;
        } else {
            return null;
        }
    }
}